package com.swu.education.controller;

import com.swu.education.entity.Course;
import com.swu.education.service.CourseService;
import com.swu.education.service.ReadExcelFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.swu.education.entity.User;
import org.springframework.web.multipart.MultipartFile;

/*import java.text.SimpleDateFormat;
import java.util.Date;*/
import java.util.List;

/**
 * Created by nana on 2018/3/13.
 */
@RestController
public class CourseController {

  @Autowired
  CourseService courseService;

  @Autowired
  ReadExcelFileService readExcelFileService;

  //获取session存储的user
  @RequestMapping(value = "/getUserFromSession")
  public User getUserFromSession(HttpSession session){
    return (User)session.getAttribute("user");
  }

  //根据课程id 查询课程
  @RequestMapping(value = "/getCourseById")
  public Course getCourseById(@RequestParam(value = "courseId") int courseId) throws Exception {

    return courseService.getCourseByCourseId(courseId);
  }

  //根据用户id 查询该用户的所有课程
  @RequestMapping(value = "/getAllCourse")
  public List<Course> getAllCourse(HttpSession session) throws Exception {
    User user = (User) session.getAttribute("user");
    return courseService.getCourseById(user.getId());
  }

  //创建课程
  @RequestMapping(value = "/createCourse")
  public void createCourse(HttpSession session,
                           @RequestParam(value = "courseName") String courseName,
                             @RequestParam(value = "startTime") String startTime,
                             @RequestParam(value = "endTime") String endTime,
                             @RequestParam(value = "file") MultipartFile file){
    User user = (User) session.getAttribute("user");
    if(file!=null){
      Course course =  courseService.createCourseByTeacherId(user.getId(),courseName,startTime,endTime);
      readExcelFileService.importStudent(course.getCourseId(),file);
    }else {
      courseService.createCourseByTeacherId(user.getId(),courseName,startTime,endTime);
    }
  }

  //删除课程
  @RequestMapping(value = "/deleteCourse")
  public void deleteCourse(@RequestParam(value = "courseId") int courseId){
    courseService.deleteCourseById(courseId);
  }

  //修改课程信息
  @RequestMapping(value = "/modifyCourse")
  public void modifyCourse(@RequestParam(value = "courseId") int courseId,
          @RequestParam(value = "courseName") String courseName,
                           @RequestParam(value = "startTime") String startTime,
                           @RequestParam(value = "endTime") String endTime,
                           @RequestParam(value = "file") MultipartFile file){
    System.out.println("courseId="+courseId+" courseName: "+courseName);
    if(file!=null){
      //file不为空
      System.out.println("file is full");
      courseService.modifyCourseInfoById(courseId,courseName,startTime,endTime,"full");
      //重新添加课程的用户信息
      System.out.println("add userInfo again");
      readExcelFileService.importStudent(courseId,file);
    }
    else{
      System.out.println("file is not full");
      courseService.modifyCourseInfoById(courseId,courseName,startTime,endTime,"black");
    }
  }

  //获取学生的课程信息
  @RequestMapping(value = "/getAllCourseByStudent")
  public List<Course> getAllCourseByStudent(HttpSession session){
    User user = (User) session.getAttribute("user");
    return courseService.getStudentCourseList(user.getId());
  }
}
